#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e4 + 10;

int n;
int a[N * 2];
int dis[N * 2];
unordered_map<int, int> h;

struct node
{
	int l, r;
}b[N];

int main()
{
	cin >> n;
	int cnt = 0;
	for(int i = 1;i <= n;i ++)
	{
		int x, y; cin >> x >> y;
		dis[++ cnt] = x;
		dis[++ cnt] = y;
		b[i].l = x, b[i].r = y;
	}
	// 排序 + 去重
	sort(dis + 1, dis + 1 + cnt);
	int pos = unique(dis + 1, dis + 1 + cnt) - (dis + 1);
	for(int i = 1;i <= pos;i ++)
	{
		int x = dis[i];
		h[x] = i;
	}

	// 差分
	for(int i = 1;i <= n;i ++)
	{
		int l = b[i].l, r = b[i].r;
		int x = h[l], y = h[r];
		a[x] += 1, a[y] -= 1;
	}
	for(int i = 1;i <= pos;i ++) a[i] += a[i - 1];

	int ans = 0;
	for(int i = 1;i <= pos;i ++)
	{
		int j = i + 1;
		while(j <= pos && a[j] > 0) j ++;
		int x = dis[i], y = dis[j];
		ans += y - x;
		i = j;
	}
	cout << ans << endl;
	return 0;
}